<?php
/*
    [GRIRMS] (C) 2010 Grandison & JM Technologies

    @author kevin<hasson.lai@mobile-grandison.com>
    @version $Id: datatable.class.php 1 2010-09-15 $
*/

//ver 2 : $f->add('-')->add('姓名')->add('|')->add('textbox', 'name', '张三');


class zz_form{
	private $elements = array();
	private $position = 0;
	private $html = '';
	private $tr_flag = 0;
	private $td_flag = 0;
	private $caption = '';
	private $action = '';
	private $method = 'post';
	
	public function __construct(){

	}
	
	public function set_caption($caption){
		$this->caption = $caption;
		return $this;
	}
	
	public function set_action($action){
		$this->action = $action;
		return $this;
	}
	
	public function set_method($method){
		$this->method = $method;
		return $this;
	}
	
	public function add_row($text = '', $attr='', $attr2=''){
		if($attr == '') $attr='align="right"';
		if($this->td_flag == 1) $this->html .= "</td>";
		if($this->tr_flag == 1) $this->html .= "</tr>";
		$this->html .= "<tr $attr2><td $attr>$text";
		$this->tr_flag = 1;
		$this->td_flag = 1;
		return $this;
	}
	
	public function add_cell($text='', $attr =''){
		if($this->td_flag == 1) $this->html .= "</td>";
		$this->html .= "<td $attr>$text";
		$this->td_flag = 1;
		$this->position++;
		return $this;
	}
	
	public function add_text($text){
		$this->html .= $text;
		return $this;
	}
	
	public function mk_hidden($hidden_array){
		foreach($hidden_array as $name => $value){
			$this->elements[$name] = array(
				'type' => 'hidden',
				'position' => -1,
				'name' => $name,
				'value' => $value,
				'html' => "<input type=\"hidden\" id=\"post_$name\" name=\"post_$name\" value=\"$value\" /> "
			);
		}
		return $this;
	}
	
	public function mk_textarea($name, $value='', $extra=''){
		$html = "<textarea id=\"post_$name\" name=\"post_$name\" value=\"$value\" $extra>$value</textarea> ";
		$this->elements[$name] = array(
			'type' => 'textarea',
			'position' => $this->position,
			'name' => $name,
			'value' => $value,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function mk_textbox($name, $value='', $extra=''){
		$html = "<input type=\"text\" id=\"post_$name\" name=\"post_$name\" value=\"$value\" $extra /> ";
		$this->elements[$name] = array(
			'type' => 'text',
			'position' => $this->position,
			'name' => $name,
			'value' => $value,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
		
	public function mk_pwdbox($name, $value='', $extra=''){
		$html = "<input type=\"password\" id=\"post_$name\" name=\"post_$name\" value=\"$value\" $extra /> ";
		$this->elements[$name] = array(
			'type' => 'password',
			'position' => $this->position,
			'name' => $name,
			'value' => $value,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function mk_button($name, $value='', $extra=''){
		$_name = $name == '' ? 'button'.rand(1000,9999) : $name;
		$showname = $name == '' ? '' : 'name="post_'.$_name.'"';
		$html = "<input type=\"button\" id=\"post_$_name\" $showname value=\"$value\" $extra /> ";
		$this->elements[$_name] = array(
			'type' => 'button',
			'position' => $this->position,
			'name' => $_name,
			'value' => $value,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function mk_submit($name, $value='', $extra=''){
		$_name = $name == '' ? 'button'.rand(1000,9999) : $name;
		$showname = $name == '' ? '' : 'name="post_'.$_name.'"';
		$html = "<input type=\"submit\" id=\"post_$_name\" $showname value=\"$value\" $extra /> ";
		$this->elements[$_name] = array(
			'type' => 'submit',
			'position' => $this->position,
			'name' => $_name,
			'value' => $value,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function mk_radiobox($name, $data, $default='', $extra=''){
		$html = '';
		foreach($data as $value => $text){
			$checked = '';
			if(!is_array($default)) $default = $default == '' ? array() : array($default);
			$i = 0;
			foreach($default as $v){
				if($value == $v){
					$checked = ' checked="checked"';
					break;
				}
			}
			$i++;
			$html .= "<label><input type=\"radio\" id=\"post_$name_$i\" name=\"post_{$name}[]\" value=\"$value\"$checked $extra />$text</label> ";
		}
		$this->elements[$name] = array(
			'type' => 'radio',
			'position' => $this->position,
			'name' => $name,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function mk_checkbox($name, $data, $default='', $extra=''){
		$html = '';
		$i = 0;
		foreach($data as $value => $text){
			$checked = '';
			if(!is_array($default)) $default = $default == '' ? array() : array($default);
			foreach($default as $v){
				if($value == $v){
					$checked = ' checked="checked"';
					break;
				}
			}
			$i++;
			$html .= "<label><input type=\"checkbox\" id=\"post_$name_$i\" name=\"post_{$name}[]\" value=\"$value\"$checked $extra />$text</label> ";
		}
		$this->elements[$name] = array(
			'type' => 'checkbox',
			'position' => $this->position,
			'name' => $name,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function mk_select($name, $data, $default='', $extra=''){
		$html = "<select id=\"post_$name\" name=\"post_$name\" $extra>";
		foreach($data as $value => $text){
			$selected = '';
			if(!is_array($default)) $default = $default == '' ? array() : array($default);
			foreach($default as $v){
				if($value == $v){
					$selected = ' selected="selected"';
					break;
				}
			}
			$html .= "<option value=\"$value\"$selected>$text</option> ";
		}
		$html .= "</select> ";
		$this->elements[$name] = array(
			'type' => 'select',
			'position' => $this->position,
			'name' => $name,
			'html' => $html
		);
		$this->html .= $html;
		return $this;
	}
	
	public function get($name){
		return $this->elements[$name]['html'];
	}
	
	public function get_hidden(){
		$html = '';
		foreach($this->elements as $element){
			if($element['type'] == 'hidden') $html .= $element['html'];;
		}
		return $html;
	}
	
	//暂不使用
	public function show(){
		if($this->td_flag == 1) $this->html .= "</td>";
		if($this->tr_flag == 1) $this->html .= "</tr>";
		$html = '<form id="post_form" action="'.$this->action.'" method="'.$this->method.'" class="zz-datatable">';
		$html .= $this->get_hidden();
		$html .= '<table cellspacing="1" cellpadding="4">';
		$html .= '<tr><th colspan="10">'.$this->caption.'</th></tr>';
		$html .= $this->html;
		$html .= '</table>';
		$html .= '</form>';
		return $html;
	}
}
?>
